package org.czl.leetCode1;

import java.util.ArrayList;
import java.util.List;

public class L113 {
    int goal = 0;
    List<List<Integer>> ans = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    void dfs(TreeNode root, int targetSum){
        if(root == null)return;
        goal += root.val;
        path.add(root.val);
        if(root.left == null && root.right == null){
            if(goal == targetSum){
                ans.add(new ArrayList(path));
            }
        }
        dfs(root.left, targetSum);
        dfs(root.right, targetSum);
        goal -= root.val;
        path.remove(path.size() - 1);

    }
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        dfs(root, targetSum);
        return ans;
    }
}
